package com.bootdo.goods.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.goods.domain.TGoodsShelfDO;
import com.bootdo.goods.service.TGoodsShelfService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 货架表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-24 14:13:19
 */
 
@Controller
@RequestMapping("/goods/tGoodsShelf")
public class TGoodsShelfController {
	@Autowired
	private TGoodsShelfService tGoodsShelfService;
	
	@GetMapping()
	@RequiresPermissions("goods:tGoodsShelf:tGoodsShelf")
	String TGoodsShelf(){
	    return "goods/tGoodsShelf/tGoodsShelf";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("goods:tGoodsShelf:tGoodsShelf")
	public PageUtils list(@RequestParam Map<String, Object> params){

		System.out.println("params:"+JSON.toJSON(params));
		//查询列表数据
        Query query = new Query(params);
		List<TGoodsShelfDO> tGoodsShelfList = tGoodsShelfService.list(query);
		int total = tGoodsShelfService.count(query);
		PageUtils pageUtils = new PageUtils(tGoodsShelfList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "goods/tGoodsShelf/add";
	}
	@GetMapping("/queryAllocation/{shelfId}")
	String queryAllocation(@PathVariable("shelfId") Long shelfId,Model model){
		TGoodsShelfDO tGoodsShelf = tGoodsShelfService.get(shelfId);
		model.addAttribute("shelfId",shelfId);
		model.addAttribute("tGoodsShelf",tGoodsShelf);
		return "goods/tGoodsShelf/shelfAllocation";
	}

	@GetMapping("/edit/{shelfId}")
	//@RequiresPermissions("goods:tGoodsShelf:edit")
	String edit(@PathVariable("shelfId") Long shelfId,Model model){
		TGoodsShelfDO tGoodsShelf = tGoodsShelfService.get(shelfId);
		model.addAttribute("tGoodsShelf", tGoodsShelf);
	    return "goods/tGoodsShelf/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( TGoodsShelfDO tGoodsShelf,Integer shelfLnum){
		//System.out.println("shelfLnum:"+shelfLnum);
		if(tGoodsShelfService.save(tGoodsShelf,shelfLnum)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( TGoodsShelfDO tGoodsShelf){
		tGoodsShelfService.update(tGoodsShelf);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long shelfId){
		if(tGoodsShelfService.remove(shelfId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] shelfIds){
		tGoodsShelfService.batchRemove(shelfIds);
		return R.ok();
	}
	/*      我的货架     */

	@GetMapping("my")
	String TGoodsShelfMy(){
		return "goods/tGoodsShelf/my/tGoodsShelf";
	}

	@ResponseBody
	@GetMapping("/list/my")
	//@RequiresPermissions("goods:tGoodsShelf:tGoodsShelf")
	public PageUtils listMy(@RequestParam Map<String, Object> params){
		UserDO userDO = ShiroUtils.getUser();
		params.put("providerId",userDO.getProviderId());
		//查询列表数据
		Query query = new Query(params);
		List<TGoodsShelfDO> tGoodsShelfList = tGoodsShelfService.list(query);
		int total = tGoodsShelfService.count(query);
		PageUtils pageUtils = new PageUtils(tGoodsShelfList, total);
		return pageUtils;
	}


	@GetMapping("/queryAllocation/my/{shelfId}")
	String queryAllocationMy(@PathVariable("shelfId") Long shelfId,Model model){
		TGoodsShelfDO tGoodsShelf = tGoodsShelfService.get(shelfId);
		model.addAttribute("shelfId",shelfId);
		model.addAttribute("tGoodsShelf",tGoodsShelf);
		return "goods/tGoodsShelf/my/shelfAllocation";
	}

	@GetMapping("/edit/my/{shelfId}")
		//@RequiresPermissions("goods:tGoodsShelf:edit")
	String editMy(@PathVariable("shelfId") Long shelfId,Model model){
		TGoodsShelfDO tGoodsShelf = tGoodsShelfService.get(shelfId);
		model.addAttribute("tGoodsShelf", tGoodsShelf);
		return "goods/tGoodsShelf/my/edit";
	}


	/**
	 * 货架平面图
	 */
	@GetMapping("plane")
	//@RequiresPermissions("goods:tGoodsShelf:tGoodsShelf")
	public String plane(Model model){
		return "goods/tGoodsShelf/plane";
	}
	/**
	 * 显示货架平面图
	 */
	@GetMapping("shelf")
	@ResponseBody
    public List<TGoodsShelfDO> shelf(@RequestParam String shelfArea){
		Map<String,Object> map = new HashMap<>();
		map.put("shelfArea",shelfArea);
		map.put("sort","order_by");
		map.put("sort.trim()","ASC");
		List<TGoodsShelfDO> tGoodsShelfDOS= tGoodsShelfService.list(map);
		System.out.println("tGoodsShelfDOS:"+JSON.toJSON(tGoodsShelfDOS));
		return tGoodsShelfDOS;
	}


}
